package org.peterf.foxish.util;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.peterf.foxish.activity.BugReportActivity;
import org.peterf.foxish.engine.setting.ScreenSetting;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * Provide useful activity tools.
 * 
 * @author peter
 * 
 */
public class ActivityUtil {

    /**
     * Apply the activity-specific screen setting, including the style,
     * orientation, etc.
     * 
     * @param a
     *            The activity to applying the setting to.
     * @param s
     *            The setting to be applied.
     */
    public static void applyScreenSetting(Activity a, ScreenSetting s) {
        if (s.isFullScreen()) {
            setFullScreen(a);
        }
        switch (s.getOrientation()) {
        case LANDSCAPE: {
            a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;
        }
        case PORTRAIT: {
            a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        }
        }
    }

    /**
     * Set an activity full screen.
     * 
     * @param a
     *            The activity to be set.
     */
    public static void setFullScreen(Activity a) {
        a.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        a.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        a.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    }

    /**
     * Start the default library bug report activity as a new task.
     * 
     * @param a
     *            The context that starts it.
     * @param e
     *            The exception that causes the bug.
     */
    public static void startBugReportActivity(Context a, Throwable e) {
        Intent intent = new Intent(a, BugReportActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        intent.putExtra(BugReportActivity.PARAMETER_NAME, writer.toString());
        a.startActivity(intent);
    }

    /**
     * Show a toast text for a long time.
     * 
     * @param a
     *            The activity representing the context.
     * @param msg
     *            The text to be displayed.
     */
    public static void longToast(Activity a, String msg) {
        Toast.makeText(a, msg, Toast.LENGTH_LONG).show();
    }

    /**
     * Show a toast text for a short time.
     * 
     * @param a
     *            The activity representing the context.
     * @param msg
     *            The text to be displayed.
     */
    public static void shortToast(Activity a, String msg) {
        Toast.makeText(a, msg, Toast.LENGTH_SHORT).show();
    }
    
    public static void longToast(Activity a, int msgId) {
        Toast.makeText(a, msgId, Toast.LENGTH_LONG).show();
    }
    
    public static void shortToast(Activity a, int msgId) {
        Toast.makeText(a, msgId, Toast.LENGTH_SHORT).show();
    }

}
